﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinEntity;
using EmoneyWeiXinDAL;
using System.Data;
using EmoneyWeiXinCommon;
using System.Web;
namespace EmoneyWeiXinBLL
{
    public class MenuBLL
    {
        public static List<MenuEntity> getMenuList(int weixinid)
        {
            List<MenuEntity> list = MenuDal.getMenuList(weixinid);
            if (list != null && list.Count > 0)
                list = list.Where(p => p.hisId != 0).ToList();
            list = list.GroupBy(x => x.everyKey).Select(t => t.First()).ToList();
            return list;
        }
        public static List<MenuEntity> getMenuList(int weixinid, int userid)
        {
            return MenuDal.getMenuList(weixinid, userid); ;
        }
        public static DataSet getMenuList2(int weixinid, int userid)
        {
            return MenuDal.getMenuList2(weixinid, userid); ;
        }
        public static MenuEntity getMenuListByID(int id)
        {
            var list = MenuDal.getMenuListByID(id);
            if (list != null && list.Count > 0)
                return list[0];
            return new MenuEntity();
        }
        public static DataSet getMenu_NewsReport_ListByID(int id)
        {
            return MenuDal.getMenu_NewsReport_ListByID(id);
        }
        public static List<MenuEntity> getMenuParentList(int weixinid)
        {
            return MenuDal.getMenuParentList(weixinid);
        }
        public static List<MenuEntity> getMenuChildrenList(int id, int weixinid)
        {
            return MenuDal.getMenuChildrenList(id, weixinid);
        }
        public static DataSet getMenuParentAndChildren(int weixinid)
        {
            return MenuDal.getMenuParentAndChildren(weixinid);
        }
        public static List<MenuEntity> getMenuList()
        {
            return MenuDal.getMenuList();
        }
        public static List<OperateEntity> getOperateList()
        {
            return MenuDal.getOperateList();
        }
        public static List<User_OperateEntity> getOperateList(int userid, int leftmemu_id)
        {
            return MenuDal.getOperateList(userid, leftmemu_id);
        }
        public static int UpdateMenu(MenuEntity obj, string newsreportids)
        {
            return MenuDal.UpdateMenu(obj, newsreportids);
        }
        public static int DeleteMenu(int id)
        {
            return MenuDal.DeleteMenu(id);
        }
        public static DataSet getMenuType()
        {
            return MenuDal.getMenuType();
        }


        public static int UpdateWxMenu(int weixinid)
        {
            weixinUserBLL bll = new weixinUserBLL();
            var obj = bll.getWeixinUserByID(weixinid);
            string appid = obj.AppId;
            string appsercret = obj.AppSecret;
            string hosturl = obj.servicesUrl;
            string json = createWxMenuJson(weixinid, hosturl, appid, obj.AppSecret);
            int ret = EmoneyWeiXinCommon.Menu.Create(json, appid, appsercret);
            return ret;
        }
        public static string createWxMenuJson(int weixinid, string hosturl, string appid, string appsecret)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{\"button\":[");
            IList<MenuEntity> list = MenuDal.getMenuList(weixinid);
            if (list != null && list.Count > 0)
            {
                list = list.Where(x => x.isweixinMemu == 1).ToList();
            }
            IList<MenuEntity> cdlist;
            IList<MenuEntity> plist = list.Where(x => x.hisId == 0).ToList();
            for (int i = 0; i < plist.Count; i++)
            {
                if (i != 0) sb.Append(",");
                if (plist[i].mtypename == "menu")
                {
                    sb.Append("{\"name\": \"" + plist[i].title + "\",\"sub_button\": [");
                    cdlist = list.Where(y => y.hisId == plist[i].id).ToList();
                    for (int k = 0; k < cdlist.Count; k++)
                    {
                        if (k != 0) sb.Append(",");
                        sb.Append(getWxMenuItemStr(cdlist[k], hosturl, appid, appsecret, weixinid));
                    }
                    sb.Append("]}");
                }
                else
                {
                    sb.Append(getWxMenuItemStr(plist[i], hosturl, appid, appsecret, weixinid));
                }
            }
            sb.Append("]}");
            plist = null; cdlist = null;
            return sb.ToString();
        }
        public static string getWxMenuItemStr(MenuEntity obj, string hosturl, string appid, string appsecret, int weixinId)
        {
            if (obj.mtypename == "view")
            {
                //if (obj.everyKey.ToLower().IndexOf("http://") != -1)
                //{
                //    return "{\"type\":\"view\",\"name\":\"" + obj.title + "\",\"url\":\"" + obj.everyKey + "\"}";
                //}
                //else
                //{

                //if (obj.Keyword == "benpaobagumin")
                //{
                //    var url = Config.RedirectUrl + "?goUrl=" + HttpUtility.UrlEncode(obj.linkUrl) + "&keyCode=" + obj.title + "&weixinId=" + weixinId;
                //    return "{\"type\":\"view\",\"name\":\"" + obj.title + "\",\"url\":\"" + OAuth2Helper.GetAuthorizeUrl(url, false,appid) + "\"}";
                //}
                //else
                //{
                if (obj.isJianQuan == 1)
                {
                    var url = Config.RedirectUrl + "?goUrl=" + HttpUtility.UrlEncode(obj.linkUrl) + "&keyCode=" + obj.everyKey + "&weixinId=" + weixinId;
                    return "{\"type\":\"view\",\"name\":\"" + obj.title + "\",\"url\":\"" + OAuth2Helper.GetAuthorizeUrl(url, appid) + "\"}";
                }
                else
                {
                    return "{\"type\":\"view\",\"name\":\"" + obj.title + "\",\"url\":\"" + obj.linkUrl + "\"}";
                }
                //}
                //}
            }
            else if (obj.mtypename == "click")
            {
                return "{\"type\": \"click\",\"name\": \"" + obj.title + "\",\"key\": \"" + obj.everyKey + "\"}";
            }
            else return "";
        }
    }
}
